package com.szyjjk.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.szyjjk.entity.*;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: szyjjk
 * @description: excel 生成并且导出
 * @author: yangyue
 * @create: 2020-06-16 15:19
 */
public class ExportExcelUtils {


    private String title; // 导出表格的表名

    private String[] rowName;// 导出表格的列名

    private List<Object[]> dataList = new ArrayList<Object[]>(); // 对象数组的List集合

    private HttpServletResponse response;

    // 传入要导入的数据
    public ExportExcelUtils(String title, String[] rowName, List<Object[]> dataList, HttpServletResponse response) {
        this.title = title;
        this.rowName = rowName;
        this.dataList = dataList;
        this.response = response;
    }

    // 导出数据
    public void exportData() {
        try {
            HSSFWorkbook workbook = new HSSFWorkbook(); // 创建一个excel对象
            HSSFSheet sheet = workbook.createSheet(title); // 创建表格
            HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
            // 产生表格标题行
            HSSFRow rowm = sheet.createRow(0);  // 行
            HSSFCell cellTiltle = rowm.createCell(0);  // 单元格
            // sheet样式定义
            HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook); // 头样式
            HSSFCellStyle style = this.getStyle(workbook);  // 单元格样式
            /**
             * 参数说明
             * 从0开始   第一行 第一列 都是从角标0开始
             * 行 列 行列    (0,0,0,5)  合并第一行 第一列  到第一行 第六列
             * 起始行，起始列，结束行，结束列
             *
             * new Region()  这个方法使过时的
             */
            // 合并第一行的所有列
            sheet.addMergedRegion(new CellRangeAddress(0, (short) 0, 0, (short) (rowName.length - 1)));
            cellTiltle.setCellStyle(columnTopStyle);
            cellTiltle.setCellValue(title);

            int columnNum = rowName.length;  // 表格列的长度
            HSSFRow rowRowName = sheet.createRow(1);  // 在第二行创建行
            HSSFCellStyle cells = workbook.createCellStyle();
            cells.setBottomBorderColor(HSSFColor.BLACK.index);
            rowRowName.setRowStyle(cells);

            // 循环 将列名放进去
            for (int i = 0; i < columnNum; i++) {
                HSSFCell cellRowName = rowRowName.createCell((int) i);
                cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); // 单元格类型

                HSSFRichTextString text = new HSSFRichTextString(rowName[i]);  // 得到列的值
                cellRowName.setCellValue(text); // 设置列的值
                cellRowName.setCellStyle(columnTopStyle); // 样式
            }

            // 将查询到的数据设置到对应的单元格中
            for (int i = 0; i < dataList.size(); i++) {
                Object[] obj = dataList.get(i);//遍历每个对象
                HSSFRow row = sheet.createRow(i + 2);//创建所需的行数
                row.setHeightInPoints(40);

                for (int j = 0; j < obj.length; j++) {
                    HSSFCell cell = null;   //设置单元格的数据类型
                    if (j == 0) {
                        // 第一列设置为序号
                        cell = row.createCell(j, HSSFCell.CELL_TYPE_NUMERIC);
                        cell.setCellValue(i + 1);
                    } else {
                        cell = row.createCell(j, HSSFCell.CELL_TYPE_STRING);

                        if (!"".equals(obj[j]) && obj[j] != null) {
                            if (obj[j].toString().contains("&&")) {
                                cell.setCellValue("  ");
                                drawPictureInfoExcel(workbook, patriarch, obj[j].toString(), i + 2);//i+2代表当前的行
                            } else {
                                cell.setCellValue(obj[j].toString());
                            }
                            //设置单元格的值
                        } else {
                            cell.setCellValue("  ");
                        }

                    }
                    cell.setCellStyle(style); // 样式
                }
            }
            //  让列宽随着导出的列长自动适应

            sheet.setColumnWidth(0, 8 * 256);  //调整第一列宽度
            sheet.setColumnWidth(1, 8 * 256);
            sheet.setColumnWidth(2, 20 * 256);  ///调整第二列宽度
            sheet.setColumnWidth(3, 20 * 256);
            sheet.setColumnWidth(4, 20 * 256);
            sheet.setColumnWidth(5, 21 * 256);
            sheet.setColumnWidth(6, 20 * 256);

            if (workbook != null) {
                try {
                    // excel 表文件名
                    String fileName = title + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";
                    String fileName11 = URLEncoder.encode(fileName, "UTF-8");
                    String fileName12 = java.net.URLDecoder.decode(fileName11, "UTF-8");
                    String headStr = "attachment; filename=\"" + fileName12 + "\"";
                    response.setContentType("APPLICATION/OCTET-STREAM");
                    // response.setHeader("Content-Disposition", headStr);
                    response.setHeader("Content-Disposition",
                            "attachment;filename=" + new String(fileName12.getBytes("gb2312"), "ISO8859-1"));
                    OutputStream out = response.getOutputStream();
                    workbook.write(out);
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void drawPictureInfoExcel(HSSFWorkbook wb, HSSFPatriarch patriarch, String pictureUrl, int rowIndex) {
        //rowIndex代表当前行
        try {
            if (pictureUrl != null) {
//                URL url = new URL(pictureUrl);//获取人员照片的地址
//                //打开链接
//                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//                //设置请求方式为"GET"
//                conn.setRequestMethod("GET");
//                //超时响应时间为5秒
//                conn.setConnectTimeout(5 * 1000);
//                //通过输入流获取图片数据
//                InputStream inStream = conn.getInputStream();
                //得到图片的二进制数据，以二进制封装得到数据，具有通用性
//                byte[] data = readInputStream(inStream);
                //anchor主要用于设置图片的属性
                String []spile=pictureUrl.split("&&");
                File file=new File(spile[1]+"\\"+spile[0]);
                byte[] data = null;
                if(file.exists()){
                    data= FileUtils.readFileToByteArray(new File(spile[1]+"\\"+spile[0]));
                }else{
                    return;
                }
               // byte[] data = FileUtils.readFileToByteArray(new File(spile[1]+"\\"+spile[0]));
                int gezi=Integer.parseInt(spile[2]);
                HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 255, (short)gezi , rowIndex, (short)gezi,
                        rowIndex);
                //Sets the anchor type （图片在单元格的位置）
                //0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
                anchor.setAnchorType(0);
                patriarch.createPicture(anchor, wb.addPicture(data, HSSFWorkbook.PICTURE_TYPE_JPEG));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        //创建一个Buffer字符串
        byte[] buffer = new byte[1024];
        //每次读取的字符串长度，如果为-1，代表全部读取完毕
        int len = 0;
        //使用一个输入流从buffer里把数据读取出来
        while ((len = inStream.read(buffer)) != -1) {
            //用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        //关闭输入流
        inStream.close();
        //把outStream里的数据写入内存
        return outStream.toByteArray();
    }

    public HSSFCellStyle getStyle(HSSFWorkbook workbook) {
        // 设置字体
        HSSFFont font = workbook.createFont();
        //设置字体大小
        font.setFontHeightInPoints((short) 9);
        //字体加粗
        //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        //设置字体名字
        font.setFontName("Courier New");
        //设置样式;
        HSSFCellStyle style = workbook.createCellStyle();
        //设置底边框;
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        //设置底边框颜色;
        style.setBottomBorderColor(HSSFColor.BLACK.index);
        //设置左边框;
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        //设置左边框颜色;
        style.setLeftBorderColor(HSSFColor.BLACK.index);
        //设置右边框;
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        //设置右边框颜色;
        style.setRightBorderColor(HSSFColor.BLACK.index);
        //设置顶边框;
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        //设置顶边框颜色;
        style.setTopBorderColor(HSSFColor.BLACK.index);
        //在样式用应用设置的字体;
        style.setFont(font);
        //设置自动换行;
        style.setWrapText(false);
        //设置水平对齐的样式为居中对齐;
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //设置垂直对齐的样式为居中对齐;
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        return style;
    }

    /**
     * @return com.szyjjk.util.ExportExcelUtils
     * @Author yangyue
     * @Description //TODO 处理excl的数据
     * @Date 14:28 2020/6/17
     * @Param [siteType, rueslt, response]
     **/
    public static ExportExcelUtils returnHistory(String siteType, JSONObject rueslt, HttpServletResponse response, HttpServletRequest request) {
        List<Object[]> dataList = new ArrayList<Object[]>();
        String[] rowsName = null;
        JSONArray runSiteHKObsPageInfo = rueslt.getJSONArray("list");
        switch (siteType) {
            case "香港天文台":
                rowsName = new String[]{"序号", "站点名称", "时间", "气压", "最高气温", "平均气温", "最低气温", "平均露点温度", "相对湿度", "平均云量", "总雨量", "盛行风向", "平均风速"};
                for (int i = 0; i < runSiteHKObsPageInfo.size(); i++) {
                    ObjectMapper objectMapper = new ObjectMapper();
                    RunSiteHKObs runSiteHKObs = objectMapper.convertValue(runSiteHKObsPageInfo.get(i), RunSiteHKObs.class);
                    Object[] objs = new Object[rowsName.length];
                    objs[0] = i + 1;
                    objs[1] = runSiteHKObs.getSiteId();
                    objs[2] = DateUtil.dateToString2(runSiteHKObs.getObsTime());
                    objs[3] = runSiteHKObs.getObsQiya();
                    objs[4] = runSiteHKObs.getObsQiwenMax();
                    objs[5] = runSiteHKObs.getObsQiwenPj();
                    objs[6] = runSiteHKObs.getObsQiwenMin();
                    objs[7] = runSiteHKObs.getObsLudian();
                    objs[8] = runSiteHKObs.getObsShidu();
                    objs[9] = runSiteHKObs.getObsYunliang();
                    objs[10] = runSiteHKObs.getObsYuliang();
                    objs[11] = runSiteHKObs.getObsFengxiang();
                    objs[12] = runSiteHKObs.getObsFengsu();
                    dataList.add(objs);
                }
                break;
            case "深圳气象数据网":
                rowsName = new String[]{"序号", "站点名称", "时间", "类别", "00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00"};

                for (int i = 0; i < runSiteHKObsPageInfo.size(); i++) {
                    ObjectMapper objectMapper = new ObjectMapper();
                    RunSiteData resultDate = objectMapper.convertValue(runSiteHKObsPageInfo.get(i), RunSiteData.class);
                    Object[] objs = new Object[rowsName.length];
                    objs[0] = i + 1;
                    objs[1] = resultDate.getSiteId();
                    objs[2] = DateUtil.dateToString2(resultDate.getDataTime());
                    objs[3] = resultDate.getDataType();
                    objs[4] = resultDate.getDataH0();
                    objs[5] = resultDate.getDataH1();
                    objs[6] = resultDate.getDataH2();
                    objs[7] = resultDate.getDataH3();
                    objs[8] = resultDate.getDataH4();
                    objs[9] = resultDate.getDataH5();
                    objs[10] = resultDate.getDataH7();
                    objs[11] = resultDate.getDataH8();
                    objs[12] = resultDate.getDataH9();
                    objs[13] = resultDate.getDataH10();
                    objs[14] = resultDate.getDataH11();
                    objs[15] = resultDate.getDataH12();
                    objs[16] = resultDate.getDataH13();
                    objs[17] = resultDate.getDataH14();
                    objs[18] = resultDate.getDataH15();
                    objs[19] = resultDate.getDataH16();
                    objs[20] = resultDate.getDataH17();
                    objs[21] = resultDate.getDataH18();
                    objs[22] = resultDate.getDataH19();
                    objs[23] = resultDate.getDataH20();
                    objs[23] = resultDate.getDataH21();
                    objs[23] = resultDate.getDataH22();
                    objs[23] = resultDate.getDataH23();
                    dataList.add(objs);
                }
                break;
            case "水质数据":
                rowsName = new String[]{"序号", "站点名称", "时间", "溶解氧 (毫克/升)", "大肠桿菌 (菌落数/100毫升)"};
                for (int i = 0; i < runSiteHKObsPageInfo.size(); i++) {
                    ObjectMapper objectMapper = new ObjectMapper();
                    RunWater resultDate = objectMapper.convertValue(runSiteHKObsPageInfo.get(i), RunWater.class);
                    Object[] objs = new Object[rowsName.length];
                    objs[0] = i + 1;
                    objs[1] = resultDate.getSiteId();
                    objs[2] = DateUtil.dateToString2(resultDate.getWaterTime());
                    objs[3] = resultDate.getWaterOxygen();
                    objs[4] = resultDate.getWaterGerm();
                    dataList.add(objs);
                }
                break;
            case "潮汐数据":
                rowsName = new String[]{"序号", "站点名称", "日期", "时间1", "高度1", "时间2", "高度2", "时间3", "高度3", "时间4", "高度4"};
                for (int i = 0; i < runSiteHKObsPageInfo.size(); i++) {
                    ObjectMapper objectMapper = new ObjectMapper();
                    RunTide resultDate = objectMapper.convertValue(runSiteHKObsPageInfo.get(i), RunTide.class);
                    Object[] objs = new Object[rowsName.length];
                    objs[0] = i + 1;
                    objs[1] = resultDate.getSideId();
                    objs[2] = DateUtil.dateToString1(resultDate.getTideDate());
                    objs[3] = DateUtil.dateToString2(resultDate.getTideTime1());
                    objs[4] = resultDate.getTideHight1();
                    objs[5] = DateUtil.dateToString2(resultDate.getTideTime2());
                    objs[6] = resultDate.getTideHight2();
                    objs[7] = DateUtil.dateToString2(resultDate.getTideTime3());
                    objs[8] = resultDate.getTideHight3();
                    objs[9] = DateUtil.dateToString2(resultDate.getTideTime4());
                    objs[10] = resultDate.getTideHight4();
                    dataList.add(objs);
                }
                break;
            case "太阳辐射":
                rowsName = new String[]{"序号", "站点名称", "日期", "太阳辐射总量", "太阳直射量"};
                for (int i = 0; i < runSiteHKObsPageInfo.size(); i++) {
                    ObjectMapper objectMapper = new ObjectMapper();
                    RunSun resultDate = objectMapper.convertValue(runSiteHKObsPageInfo.get(i), RunSun.class);
                    Object[] objs = new Object[rowsName.length];
                    objs[0] = i + 1;
                    objs[1] = resultDate.getSiteId();
                    objs[2] = DateUtil.dateToString1(resultDate.getSunTime());
//                    String path = request.getScheme() + "://" + request.getServerName() + ":" + request.getLocalPort();
//                    //通过截取之后在来算路径
//                    int indexs = resultDate.getSunDirectPath().lastIndexOf("\\");
//                    try {
//                        objs[3] =path + "/home/readPartImage.do?address=" + resultDate.getSunGrossPath().substring(indexs + 1) + "&FilePath=" + URLEncoder.encode(resultDate.getSunGrossPath().substring(0, indexs + 1), "GBK");
//                        objs[4] =path + "/home/readPartImage.do?address=" + resultDate.getSunDirectPath().substring(indexs + 1) + "&FilePath=" + URLEncoder.encode(resultDate.getSunDirectPath().substring(0, indexs + 1), "GBK");
//                    }
//                    catch (UnsupportedEncodingException e) {
//                        e.printStackTrace();
//                    }

                    //现在需要对之前处理过的数据进行还原处理
                    //http://localhost:4089/home/readPartImage.do?address=ABT861WZXSKPTTPKXZBG04YN6UIWNZF7.png&FilePath=D%3A%5CTemp%5C
                    //作孽啊
                    if (resultDate.getSunGrossPath() != null) {
                        String[] spileString = resultDate.getSunGrossPath().split("address=")[1].split("&FilePath=");
                        String SunGrossPathFileName = spileString[0];
                        String path = "";
                        try {
                            path = java.net.URLDecoder.decode(spileString[1], "GBK");
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        String SunDirectPathFileName = resultDate.getSunDirectPath().split("address=")[1].split("&FilePath=")[0];

                        objs[3] = SunGrossPathFileName + "&&" + path + "&&3";
                        objs[4] = SunDirectPathFileName + "&&" + path + "&&4";
                    }
                    dataList.add(objs);
                }
                break;
        }
        switch (siteType) {
            case "香港天文台":
                siteType="HongKongObservatory";
                break;
            case "潮汐数据":
                siteType="Tide";
                break;
            case "水质数据":
                siteType="WaterQuality";
                break;
            case "深圳气象数据网":
                siteType="ShenzhenMeteorologicalDataNetwork";
                break;
            case "太阳辐射":
                siteType="SolarRadiation";
                break;
        }
        return new ExportExcelUtils(siteType, rowsName, dataList, response);
    }


    public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {

        // 设置字体
        HSSFFont font = workbook.createFont();
        //设置字体大小
        font.setFontHeightInPoints((short) 10);
        //字体加粗
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        //设置字体名字
        font.setFontName("Courier New");
        //设置样式;
        HSSFCellStyle style = workbook.createCellStyle();
        //设置底边框;
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        //设置底边框颜色;
        style.setBottomBorderColor(HSSFColor.BLACK.index);
        //设置左边框;
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        //设置左边框颜色;
        style.setLeftBorderColor(HSSFColor.BLACK.index);
        //设置右边框;
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        //设置右边框颜色;
        style.setRightBorderColor(HSSFColor.BLACK.index);
        //设置顶边框;
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        //设置顶边框颜色;
        style.setTopBorderColor(HSSFColor.BLACK.index);
        //在样式用应用设置的字体;
        style.setFont(font);
        //设置自动换行;
        style.setWrapText(false);
        //设置水平对齐的样式为居中对齐;
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //设置垂直对齐的样式为居中对齐;
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        return style;

    }


}